package com.gtw.modules.sys.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.entity.*;
import com.gtw.modules.sys.DTO.CommodityPropertyDTO;
import com.gtw.modules.sys.DTO.MemberCommodityPropertyDTO;
import com.gtw.modules.sys.service.SysMemberCommodityPropertyService;
import com.gtw.modules.sys.service.SysMemberCommodityService;
import com.gtw.modules.sys.service.SysMemberPropertyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 会员专区商品属性表
 * 
 * @author wuye
 * @email www.baidu.com
 * @date 2018-06-11 11:48:41
 */
@RestController
@RequestMapping("/sys/memberproperty")
public class SysMemberPropertyController extends AbstractController{
	@Autowired
	private SysMemberPropertyService memberPropertyService;

	@Autowired
	private SysMemberCommodityService memberCommodityService;

	@Autowired
	private SysMemberCommodityPropertyService memberCommodityPropertyService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:memberproperty:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<MemberPropertyEntity> memberPropertyList = memberPropertyService.queryList(query);
		int total = memberPropertyService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(memberPropertyList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 查询所有属性
	 * @return
	 */
	@RequestMapping("/findAll")
	public R findAll(){
		List<MemberPropertyEntity> list = memberPropertyService.findaLLProperty();
		return R.ok().put("list",list);
	}

	/**
	 * 为商品添加属性
	 * @return
	 */
	@RequestMapping("/saveCommodityProperty")
	public R saveCommodityProperty(@RequestParam Integer propertyId,@RequestParam Integer commodityId,@RequestParam Integer num){
		MemberCommodityPropertyEntity tbCommodityProperty = new MemberCommodityPropertyEntity();
		tbCommodityProperty.setPropertynum(num);
		tbCommodityProperty.setPropertyid(propertyId);
		tbCommodityProperty.setCommodityid(commodityId);
		tbCommodityProperty.setCreateuser(getUser().getUsername());
		tbCommodityProperty.setCreatedate(new Date());
		tbCommodityProperty.setUpdatedate(new Date());
		tbCommodityProperty.setUpdateuser(getUser().getUsername());
		tbCommodityProperty.setIsdelete(false);
		memberCommodityPropertyService.save(tbCommodityProperty);
		return R.ok();
	}

	/**
	 * 为商品修改属性
	 * @return
	 */
	@RequestMapping("/updateCommodityProperty")
	public R updateCommodityProperty(@RequestParam Integer id,@RequestParam Integer propertyId,@RequestParam Integer commodityId,@RequestParam Integer num){
		MemberCommodityPropertyEntity tbCommodityProperty = memberCommodityPropertyService.queryObject(id);
		tbCommodityProperty.setPropertynum(num);
		tbCommodityProperty.setPropertyid(propertyId);
		tbCommodityProperty.setCommodityid(commodityId);
		tbCommodityProperty.setUpdatedate(new Date());
		tbCommodityProperty.setUpdateuser(getUser().getUsername());
		memberCommodityPropertyService.update(tbCommodityProperty);
		return R.ok();
	}
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:memberproperty:info")
	public R info(@PathVariable("id") Integer id){
		MemberPropertyEntity memberProperty = memberPropertyService.queryObject(id);
		
		return R.ok().put("memberProperty", memberProperty);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:memberproperty:save")
	public R save(@RequestBody MemberPropertyEntity memberProperty){
		memberProperty.setCreatedate(new Date());
		memberProperty.setIsdelete(false);
		memberProperty.setClassid(0);
		memberPropertyService.save(memberProperty);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:memberproperty:update")
	public R update(@RequestBody MemberPropertyEntity memberProperty){
		memberPropertyService.update(memberProperty);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:memberproperty:delete")
	public R delete(@RequestBody Integer[] ids){
		memberPropertyService.deleteBatch(ids);
		
		return R.ok();
	}

	/**
	 * 根据商品ID查询
	 * @return
	 */
	@RequestMapping("/findByCommodityId")
	public R findByCommodityId(@RequestParam Integer commodityid){
		List<MemberCommodityPropertyEntity> list = memberCommodityPropertyService.foundByCommodityid(commodityid);
		List<MemberCommodityPropertyDTO> listdto = new ArrayList<>();
		for (MemberCommodityPropertyEntity tcp: list) {
			MemberCommodityPropertyDTO dto = new MemberCommodityPropertyDTO();
			MemberCommodityEntity tbCommodity = memberCommodityService.queryObject(tcp.getCommodityid());
			MemberPropertyEntity tbProperty = memberPropertyService.queryObject(tcp.getPropertyid());
			MemberCommodityPropertyEntity tbCommodityProperty = tcp;
			dto.setMemberCommodityEntity(tbCommodity);
			dto.setMemberPropertyEntity(tbProperty);
			dto.setMemberCommodityPropertyEntity(tbCommodityProperty);
			listdto.add(dto);
		}
		return R.ok().put("listdto",listdto);
	}
	
}
